package com.mission.schedule.tools.toexcel

import android.content.Context
import android.os.Environment
import android.widget.Toast
import com.mission.schedule.applcation.App
import com.mission.schedule.bean.qd606.NoteTitleDetailBean
import com.mission.schedule.entity.CLRepeatTable
import com.mission.schedule.entity.ScheduleTable
import jxl.Workbook
import jxl.format.Colour
import jxl.write.*
import java.io.File
import java.io.FileOutputStream

class ExcelUtil {


    companion object {

        var root = Environment.getExternalStorageDirectory().path
        var calender_title = arrayListOf("日期", "时间", "内容", "创建时间", "来自")
        var repeat_title = arrayListOf("日期", "内容", "时间", "创建时间", "类型")
        var note_title = arrayListOf("标题", "创建时间", "最后更新时间", "来自")


        /**
         * @param context
         * @param type 0 全部 1日程和重复 2 清单
         * @param maps
         * @param excel_name
         */
        fun WriteExcel(context: Context, type: Int, maps: List<List<Map<String, String>>>?, sheet_names: List<String>,
                       lists: List<List<NoteTitleDetailBean.TDelListBean>>?, excel_name: String, isChild: Boolean, uid: String) : File? {

            if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
                Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show()
                return null
            }


            val file: File
            val dir = File(context.getExternalFilesDir(null)!!.path)
            file = File(dir, "$excel_name.xls")
            if (!dir.exists()) {
                dir.mkdirs()
            }

            // 创建Excel工作表
            val wwb: WritableWorkbook
            val os = FileOutputStream(file)
            wwb = Workbook.createWorkbook(os)

            when (type) {
                0 -> {
                    if (isChild) {
                        sheet_names.forEachIndexed { i, name ->
                            val sheet = wwb.createSheet("$name(子项)", i + 4)
                            if (i > 1) {
                                lists!![i - 2].forEachIndexed { cell, bean ->
                                    val label = Label(cell, 0, bean.titles, getHeader())
                                    sheet.addCell(label)
                                    val list = App.getDBcApplication().getTitleNoteDetailData(bean.uid.toString(), bean.titleId, false)
                                    list.forEachIndexed { index, listBean ->
                                        val titles = Label(cell, index + 1, listBean.contents, getCell())
                                        sheet.addCell(titles)
                                    }
                                }
                            } else {
                                maps!![i].forEachIndexed { cell, map ->
                                    if (i == 0) {
                                        val label = Label(cell, 0, map[ScheduleTable.schContent], getHeader())
                                        sheet.addCell(label)
                                        val list = App.getDBcApplication().getCalenderNoteDetailData(uid, map[ScheduleTable.schSourceDesc], false)
                                        list.forEachIndexed { index, itemsBean ->
                                            val titles = Label(cell, index + 1, itemsBean.contents, getCell())
                                            sheet.addCell(titles)
                                        }
                                    } else {
                                        val label = Label(cell, 0, map[CLRepeatTable.repContent], getHeader())
                                        sheet.addCell(label)
                                        val list = App.getDBcApplication().getCalenderNoteDetailData(uid, map[CLRepeatTable.repSourceDesc], false)
                                        list.forEachIndexed { index, itemsBean ->
                                            val titles = Label(cell, index + 1, itemsBean.contents, getCell())
                                            sheet.addCell(titles)
                                        }
                                    }
                                }
                            }
                        }


                    }
                    sheet_names.forEachIndexed { i, name ->
                        val sheet = wwb.createSheet(name, i)
                        if (i > 1) {

                            note_title.forEachIndexed { index, s ->
                                val label = Label(index, 0, s, getHeader())
                                sheet.addCell(label)
                            }
                            lists!![i - 2].forEachIndexed { index, bean ->
                                val titles = Label(0, index + 1, bean.titles, getCell())
                                val createTime = Label(1, index + 1, bean.createTime, getCell())
                                val changeTime = Label(2, index + 1, bean.changeTime, getCell())
                                val pname = Label(3, index + 1, bean.pname, getCell())

                                sheet.addCell(titles)
                                sheet.addCell(createTime)
                                sheet.addCell(changeTime)
                                sheet.addCell(pname)
                            }
                        } else {
                            if (i == 0) {
                                calender_title.forEachIndexed { index, s ->
                                    val label = Label(index, 0, s, getHeader())
                                    sheet.addCell(label)
                                }
                            } else {
                                repeat_title.forEachIndexed { index, s ->
                                    val label = Label(index, 0, s, getHeader())
                                    sheet.addCell(label)
                                }
                            }

                            maps!![i].forEachIndexed { index, map ->
                                if (i == 0) {
                                    val date = Label(0, index + 1, map[ScheduleTable.schDate], getCell())
                                    val time = Label(1, index + 1, map[ScheduleTable.schTime], getCell())
                                    val content = Label(2, index + 1, map[ScheduleTable.schContent], getCell())
                                    val create_time = Label(3, index + 1, map[ScheduleTable.schCreateTime], getCell())
                                    val reco_name = Label(4, index + 1, map[ScheduleTable.schcRecommendName], getCell())

                                    sheet.addCell(date)
                                    sheet.addCell(time)
                                    sheet.addCell(content)
                                    sheet.addCell(create_time)
                                    sheet.addCell(reco_name)
                                } else {
                                    val next_time = Label(0, index + 1, map[CLRepeatTable.repNextCreatedTime], getCell())
                                    val content = Label(1, index + 1, map[CLRepeatTable.repContent], getCell())
                                    val type_pa = Label(2, index + 1, map[CLRepeatTable.repTypeParameter], getCell())
                                    val create_time = Label(3, index + 1, map[CLRepeatTable.repCreateTime], getCell())
                                    var type = when (map[CLRepeatTable.repType]) {
                                        "1" -> "每天"
                                        "2" -> "每周"
                                        "3" -> "每月"
                                        "4" -> "每年"
                                        "5" -> "工作日"
                                        "6" -> "每年(农历)"
                                        else -> {
                                            "每天"
                                        }
                                    }
                                    val type_name = Label(4, index + 1, type, getCell())

                                    sheet.addCell(next_time)
                                    sheet.addCell(content)
                                    sheet.addCell(type_pa)
                                    sheet.addCell(create_time)
                                    sheet.addCell(type_name)
                                }
                            }
                        }

                    }
                }
            // 重复和日程
                1 -> {
                    calender_repeat(wwb, maps, sheet_names, isChild, uid)
                }
            // 清单
                2 -> {
                    note(wwb, lists, sheet_names, isChild)
                }
            }

            wwb.write()
            wwb.close()

            Toast.makeText(context, "写入完成", Toast.LENGTH_LONG).show()

            return file
        }

        fun note(wwb: WritableWorkbook, lists: List<List<NoteTitleDetailBean.TDelListBean>>?, sheet_names: List<String>, isChild: Boolean) {

            if (isChild) {
                sheet_names.forEachIndexed { i, name ->
                    val sheet = wwb.createSheet("$name(子项)", i + 2)
                    lists!![i].forEachIndexed { cell, bean ->
                        val label = Label(cell, 0, bean.titles, getHeader())
                        sheet.addCell(label)
                        val list = App.getDBcApplication().getTitleNoteDetailData(bean.uid.toString(), bean.titleId, false)
                        list.forEachIndexed { index, listBean ->
                            val titles = Label(cell, index + 1, listBean.contents, getCell())
                            sheet.addCell(titles)
                        }
                    }
                }
            }

            sheet_names.forEachIndexed { i, name ->
                val sheet = wwb.createSheet(name, i)
                note_title.forEachIndexed { index, s ->
                    val label = Label(index, 0, s, getHeader())
                    sheet.addCell(label)
                }
                lists!![i].forEachIndexed { index, bean ->
                    val titles = Label(0, index + 1, bean.titles, getCell())
                    val createTime = Label(1, index + 1, bean.createTime, getCell())
                    val changeTime = Label(2, index + 1, bean.changeTime, getCell())
                    val pname = Label(3, index + 1, bean.pname, getCell())

                    sheet.addCell(titles)
                    sheet.addCell(createTime)
                    sheet.addCell(changeTime)
                    sheet.addCell(pname)
                }
            }

        }

        fun calender_repeat(wwb: WritableWorkbook, maps: List<List<Map<String, String>>>?, sheet_names: List<String>, isChild: Boolean, uid: String) {

            sheet_names.forEachIndexed { i, name ->
                val sheet = wwb.createSheet("$name(子项)", i + 2)

                maps!![i].forEachIndexed { cell, map ->
                    if (i == 0) {
                        val label = Label(cell, 0, map[ScheduleTable.schContent], getHeader())
                        sheet.addCell(label)
                        val list = App.getDBcApplication().getCalenderNoteDetailData(uid, map[ScheduleTable.schSourceDesc], false)
                        list.forEachIndexed { index, itemsBean ->
                            val titles = Label(cell, index + 1, itemsBean.contents, getCell())
                            sheet.addCell(titles)
                        }
                    } else {
                        val label = Label(cell, 0, map[CLRepeatTable.repContent], getHeader())
                        sheet.addCell(label)
                        val list = App.getDBcApplication().getCalenderNoteDetailData(uid, map[CLRepeatTable.repSourceDesc], false)
                        list.forEachIndexed { index, itemsBean ->
                            val titles = Label(cell, index + 1, itemsBean.contents, getCell())
                            sheet.addCell(titles)
                        }
                    }
                }
            }

            sheet_names.forEachIndexed { i, name ->
                val sheet = wwb.createSheet(name, i)
                if (i == 0) {
                    calender_title.forEachIndexed { index, s ->
                        val label = Label(index, 0, s, getHeader())
                        sheet.addCell(label)
                    }
                } else {
                    repeat_title.forEachIndexed { index, s ->
                        val label = Label(index, 0, s, getHeader())
                        sheet.addCell(label)
                    }
                }
                maps!![i].forEachIndexed { index, map ->
                    if (i == 0) {
                        val date = Label(0, index + 1, map[ScheduleTable.schDate], getCell())
                        val time = Label(1, index + 1, map[ScheduleTable.schTime], getCell())
                        val content = Label(2, index + 1, map[ScheduleTable.schContent], getCell())
                        val create_time = Label(3, index + 1, map[ScheduleTable.schCreateTime], getCell())
                        val reco_name = Label(4, index + 1, map[ScheduleTable.schcRecommendName], getCell())

                        sheet.addCell(date)
                        sheet.addCell(time)
                        sheet.addCell(content)
                        sheet.addCell(create_time)
                        sheet.addCell(reco_name)
                    } else {
                        val next_time = Label(0, index + 1, map[CLRepeatTable.repNextCreatedTime], getCell())
                        val content = Label(1, index + 1, map[CLRepeatTable.repContent], getCell())
                        val type_pa = Label(2, index + 1, map[CLRepeatTable.repTypeParameter], getCell())
                        val create_time = Label(3, index + 1, map[CLRepeatTable.repCreateTime], getCell())
                        var type = when (map[CLRepeatTable.repType]) {
                            "1" -> "每天"
                            "2" -> "每周"
                            "3" -> "每月"
                            "4" -> "每年"
                            "5" -> "工作日"
                            "6" -> "每年(农历)"
                            else -> {
                                "每天"
                            }
                        }
                        val type_name = Label(4, index + 1, type, getCell())

                        sheet.addCell(next_time)
                        sheet.addCell(content)
                        sheet.addCell(type_pa)
                        sheet.addCell(create_time)
                        sheet.addCell(type_name)
                    }
                }
            }
        }


        fun getHeader(): WritableCellFormat {
            val font = WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD)// 定义字体
            try {
                font.colour = Colour.BLACK// 蓝色字体
            } catch (e1: WriteException) {
                e1.printStackTrace()
            }

            val format = WritableCellFormat(font)
            try {
                format.alignment = jxl.format.Alignment.CENTRE// 左右居中
                format.verticalAlignment = jxl.format.VerticalAlignment.CENTRE// 上下居中
                format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK)// 黑色边框
                format.setBackground(Colour.GRAY_25)// 背景
            } catch (e: WriteException) {
                e.printStackTrace()
            }

            return format
        }

        fun getCell(): WritableCellFormat {
            val font = WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD)// 定义字体
            try {
                font.colour = Colour.BLACK
            } catch (e1: WriteException) {
                e1.printStackTrace()
            }

            val format = WritableCellFormat(font)
            try {
                format.alignment = jxl.format.Alignment.CENTRE// 左右居中
                format.verticalAlignment = jxl.format.VerticalAlignment.CENTRE// 上下居中
            } catch (e: WriteException) {
                e.printStackTrace()
            }

            return format
        }

    }
}